9 if( !defined( 'MEDIAWIKI' ) )
12 require_once( 'Image.php' );
15 * Special handling for image description pages
18 class ImagePage
extends Article
{
20 /* private */ var $img; // Image object this page is shown for
23 global $wgUseExternalEditor, $wgOut ;
25 $this->img
= new Image( $this->mTitle
);
27 if( $this->mTitle
->getNamespace() == NS_IMAGE
) {
28 if( $this->img
->exists() ) {
31 $this->openShowImage();
33 # No need to display noarticletext, we use our own message, output in openShowImage()
34 if( $this->getID() ) {
37 # Just need to set the right headers
38 $wgOut->setArticleFlag( true );
39 $wgOut->setRobotpolicy( 'index,follow' );
40 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
41 $wgOut->addMetaTags();
45 $this->closeShowImage();
46 $this->imageHistory();
48 if( $this->img
->exists() ) {
49 $this->showEXIFdata();
57 global $wgOut, $wgShowEXIF, $wgLang;
58 $r = '<ul id="filetoc">
59 <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE
) . '</a></li>
60 <li><a href="#filehistory">' . wfMsg( 'imghistory' ) . '</a></li>
61 <li><a href="#filelinks">' . wfMsg( 'imagelinks' ) . '</a></li>' .
62 ($wgShowEXIF ?
'<li><a href="#metadata">' . wfMsg( 'metadata' ) . '</a></li>' : '') .
64 $wgOut->addHTML( $r );
67 function showEXIFdata() {
68 global $wgOut, $wgShowEXIF;
74 $exif = $this->img
->getExifData();
75 if( count ( $exif ) == 0 ) {
76 return; # No EXIF data available
80 $r = '<h2 id="metadata">'. htmlspecialchars( wfMsg( 'metadata' ) ) . "</h2>\n";
81 $r .= "<table class='metadata'>\n" ;
83 foreach( $exif as $k => $v ) {
87 $r .= '<th>' . wfMsg( 'exif-' . strtolower( $k ) ) . "</th>\n";
88 $r .= '<td>' . htmlspecialchars( $v ) . "</td>\n";
92 $r .= "<td class='spacer'> </td>\n";
97 $r .= "<th></th><td></td></tr>\n";
100 $wgOut->addHTML( $r . "</table>\n" );
103 function openShowImage()
105 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
106 $wgUseImageResize, $wgRepositoryBaseUrl,
107 $wgUseExternalEditor, $wgServer;
108 $full_url = $this->img
->getViewURL();
112 if( $wgUser->getOption( 'imagesize' ) == '' ) {
113 $sizeSel = User
::getDefaultOption( 'imagesize' );
115 $sizeSel = IntVal( $wgUser->getOption( 'imagesize' ) );
117 if( !isset( $wgImageLimits[$sizeSel] ) ) {
118 $sizeSel = User
::getDefaultOption( 'imagesize' );
120 $max = $wgImageLimits[$sizeSel];
122 $maxHeight = $max[1];
123 $sk = $wgUser->getSkin();
125 if ( $this->img
->exists() ) {
126 if ( $this->img
->getType() ) {
128 $width = $this->img
->getWidth();
129 $height = $this->img
->getHeight();
130 # "Download high res version" link below the image
131 $msg = wfMsg('showbigimage', $width, $height, intval( $this->img
->getSize()/1024 ) );
132 if ( $width > $maxWidth ) {
133 $height = floor( $height * $maxWidth / $width );
136 if ( $height > $maxHeight ) {
137 $width = floor( $width * $maxHeight / $height );
138 $height = $maxHeight;
140 if ( $width != $this->img
->getWidth() ||
$height != $this->img
->getHeight() ) {
141 if( $wgUseImageResize ) {
142 $thumbnail = $this->img
->getThumbnail( $width );
143 $url = $thumbnail->getUrl();
145 # No resize ability? Show the full image, but scale
146 # it down in the browser so it fits on the page.
149 $anchoropen = "<a href=\"{$full_url}\">";
150 $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
154 $s = '<div class="fullImageLink" id="file">' . $anchoropen .
155 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
156 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>';
158 $s = "<div class=\"fullMedia\">" . $sk->makeMediaLink( $this->img
->getName(),'' ) . '</div>';
160 $wgOut->addHTML( $s );
161 if($this->img
->fromSharedDirectory
) {
162 $sharedtext="<div class=\"sharedUploadNotice\">" . wfMsg("sharedupload");
163 if($wgRepositoryBaseUrl) {
164 $sharedtext .= " ". wfMsg("shareduploadwiki",$wgRepositoryBaseUrl . urlencode($this->mTitle
->getDBkey()));
166 $sharedtext.="</div>";
167 $wgOut->addWikiText($sharedtext);
171 # Image does not exist
172 $wgOut->addWikiText( wfMsg( 'noimage', $this->getUploadUrl() ) );
176 function getUploadUrl() {
178 $uploadTitle = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
179 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
183 function uploadLinksBox()
185 global $wgUser,$wgOut;
186 $sk = $wgUser->getSkin();
187 $wgOut->addHTML( '<br /><ul><li>' );
188 $wgOut->addWikiText( '<div>'. wfMsg( 'uploadnewversion', $this->getUploadUrl() ) .'</div>' );
189 $wgOut->addHTML( '</li><li>' );
190 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle
,
191 wfMsg( 'edit-externally' ), "action=edit&externaledit=true&mode=file" ) );
192 $wgOut->addWikiText( '<div>' . wfMsg('edit-externally-help') . '</div>' );
193 $wgOut->addHTML( '</li></ul>' );
196 function closeShowImage()
203 * If the page we've just displayed is in the "Image" namespace,
204 * we follow it with an upload history of the image and its usage.
206 function imageHistory()
208 global $wgUser, $wgOut, $wgUseExternalEditor;
210 $sk = $wgUser->getSkin();
212 $line = $this->img
->nextHistoryLine();
215 $list =& new ImageHistoryList( $sk );
216 $s = $list->beginImageHistoryList() .
217 $list->imageHistoryLine( true, $line->img_timestamp
,
218 $this->mTitle
->getDBkey(), $line->img_user
,
219 $line->img_user_text
, $line->img_size
, $line->img_description
);
221 while ( $line = $this->img
->nextHistoryLine() ) {
222 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
223 $line->oi_archive_name
, $line->img_user
,
224 $line->img_user_text
, $line->img_size
, $line->img_description
);
226 $s .= $list->endImageHistoryList();
228 $wgOut->addHTML( $s );
230 # Exist check because we don't want to show this on pages where an image
231 # doesn't exist along with the noimage message, that would suck. -ævar
232 if( $wgUseExternalEditor && $this->img
->exists() ) {
233 $this->uploadLinksBox();
238 function imageLinks()
240 global $wgUser, $wgOut;
242 $wgOut->addHTML( '<h2 id="filelinks">' . wfMsg( 'imagelinks' ) . "</h2>\n" );
244 $dbr =& wfGetDB( DB_SLAVE
);
245 $page = $dbr->tableName( 'page' );
246 $imagelinks = $dbr->tableName( 'imagelinks' );
248 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
249 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id"
250 . " LIMIT 500"; # quickie emergency brake
251 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
253 if ( 0 == $dbr->numRows( $res ) ) {
254 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
257 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
259 $sk = $wgUser->getSkin();
260 while ( $s = $dbr->fetchObject( $res ) ) {
261 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
262 $link = $sk->makeKnownLinkObj( $name, "" );
263 $wgOut->addHTML( "<li>{$link}</li>\n" );
265 $wgOut->addHTML( "</ul>\n" );
270 global $wgUser, $wgOut, $wgRequest;
272 $confirm = $wgRequest->getBool( 'wpConfirmB' );
273 $image = $wgRequest->getVal( 'image' );
274 $oldimage = $wgRequest->getVal( 'oldimage' );
276 # Only sysops can delete images. Previously ordinary users could delete
277 # old revisions, but this is no longer the case.
278 if ( !$wgUser->isAllowed('delete') ) {
279 $wgOut->sysopRequired();
282 if ( wfReadOnly() ) {
283 $wgOut->readOnlyPage();
287 # Better double-check that it hasn't been deleted yet!
288 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
289 if ( ( !is_null( $image ) )
290 && ( '' == trim( $image ) ) ) {
291 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
295 $this->img
= new Image( $this->mTitle
);
297 # Deleting old images doesn't require confirmation
298 if ( !is_null( $oldimage ) ||
$confirm ) {
299 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
302 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
307 if ( !is_null( $image ) ) {
308 $q = '&image=' . urlencode( $image );
309 } else if ( !is_null( $oldimage ) ) {
310 $q = '&oldimage=' . urlencode( $oldimage );
314 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
319 global $wgOut, $wgUser, $wgContLang, $wgRequest;
320 global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
321 $fname = 'ImagePage::doDelete';
323 $reason = $wgRequest->getVal( 'wpReason' );
324 $oldimage = $wgRequest->getVal( 'oldimage' );
326 $dbw =& wfGetDB( DB_MASTER
);
328 if ( !is_null( $oldimage ) ) {
329 if ( strlen( $oldimage ) < 16 ) {
330 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
333 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
334 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
338 # Invalidate description page cache
339 $this->mTitle
->invalidateCache();
344 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
345 $wgInternalServer.$this->mTitle
->getFullURL()
347 wfPurgeSquidServers($urlArr);
349 $this->doDeleteOldImage( $oldimage );
350 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
351 $deleted = $oldimage;
353 $image = $this->mTitle
->getDBkey();
354 $dest = wfImageDir( $image );
355 $archive = wfImageDir( $image );
357 # Delete the image file if it exists; due to sync problems
358 # or manual trimming sometimes the file will be missing.
359 $targetFile = "{$dest}/{$image}";
360 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
361 # If the deletion operation actually failed, bug out:
362 $wgOut->fileDeleteError( $targetFile );
365 $dbw->delete( 'image', array( 'img_name' => $image ) );
366 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
368 # Purge archive URLs from the squid
370 while ( $s = $dbw->fetchObject( $res ) ) {
371 $this->doDeleteOldImage( $s->oi_archive_name
);
372 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
375 # And also the HTML of all pages using this image
376 $linksTo = $this->img
->getLinksTo();
378 $u = SquidUpdate
::newFromTitles( $linksTo, $urlArr );
379 array_push( $wgPostCommitUpdateList, $u );
382 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
384 # Image itself is now gone, and database is cleaned.
385 # Now we remove the image description page.
387 $article = new Article( $this->mTitle
);
388 $article->doDeleteArticle( $reason ); # ignore errors
390 # Invalidate parser cache and client cache for pages using this image
391 # This is left until relatively late to reduce lock time
392 Title
::touchArray( $linksTo );
394 /* Delete thumbnails and refresh image metadata cache */
395 $this->img
->purgeCache();
401 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
402 $wgOut->setRobotpolicy( 'noindex,nofollow' );
404 $sk = $wgUser->getSkin();
405 $loglink = $sk->makeKnownLinkObj(
406 Title
::makeTitle( NS_SPECIAL
, 'Log/delete' ),
407 wfMsg( 'deletionlog' ) );
409 $text = wfMsg( 'deletedtext', $deleted, $loglink );
411 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
412 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
415 function doDeleteOldImage( $oldimage )
419 $name = substr( $oldimage, 15 );
420 $archive = wfImageArchiveDir( $name );
422 # Delete the image if it exists. Sometimes the file will be missing
423 # due to manual intervention or weird sync problems; treat that
424 # condition gracefully and continue to delete the database entry.
425 # Also some records may end up with an empty oi_archive_name field
426 # if the original file was missing when a new upload was made;
427 # don't try to delete the directory then!
429 $targetFile = "{$archive}/{$oldimage}";
430 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
431 # If we actually have a file and can't delete it, throw an error.
432 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
435 $log = new LogPage( 'delete' );
436 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
442 global $wgOut, $wgRequest, $wgUser;
443 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
445 $oldimage = $wgRequest->getText( 'oldimage' );
446 if ( strlen( $oldimage ) < 16 ) {
447 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
450 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
451 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
455 if ( wfReadOnly() ) {
456 $wgOut->readOnlyPage();
459 if( $wgUser->isAnon() ) {
460 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
463 if ( ! $this->mTitle
->userCanEdit() ) {
464 $wgOut->sysopRequired();
467 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
468 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
471 $name = substr( $oldimage, 15 );
473 $dest = wfImageDir( $name );
474 $archive = wfImageArchiveDir( $name );
475 $curfile = "{$dest}/{$name}";
477 if ( ! is_file( $curfile ) ) {
478 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
481 $oldver = wfTimestampNow() . "!{$name}";
483 $dbr =& wfGetDB( DB_SLAVE
);
484 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
486 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
487 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
490 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
491 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
494 # Record upload and update metadata cache
495 $img = Image
::newFromName( $name );
496 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
498 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
499 $wgOut->setRobotpolicy( 'noindex,nofollow' );
500 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
502 $descTitle = $img->getTitle();
503 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
511 class ImageHistoryList
{
512 function ImageHistoryList( &$skin ) {
513 $this->skin
=& $skin;
516 function beginImageHistoryList() {
517 $s = "\n<h2 id=\"filehistory\">" . wfMsg( 'imghistory' ) . "</h2>\n" .
518 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
522 function endImageHistoryList() {
527 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
528 global $wgUser, $wgLang, $wgContLang, $wgTitle;
530 $datetime = $wgLang->timeanddate( $timestamp, true );
531 $del = wfMsg( 'deleteimg' );
532 $delall = wfMsg( 'deleteimgcompletely' );
533 $cur = wfMsg( 'cur' );
536 $url = Image
::imageUrl( $img );
538 if ( $wgUser->isAllowed('delete') ) {
539 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
541 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
543 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
548 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
549 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
550 $token = urlencode( $wgUser->editToken( $img ) );
551 $rlink = $this->skin
->makeKnownLinkObj( $wgTitle,
552 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
553 urlencode( $img ) . "&wpEditToken=$token" );
554 $dlink = $this->skin
->makeKnownLinkObj( $wgTitle,
555 $del, 'action=delete&oldimage=' . urlencode( $img ) .
556 "&wpEditToken=$token" );
558 # Having live active links for non-logged in users
559 # means that bots and spiders crawling our site can
560 # inadvertently change content. Baaaad idea.
561 $rlink = wfMsg( 'revertimg' );
566 $userlink = $usertext;
568 $userlink = $this->skin
->makeLinkObj(
569 Title
::makeTitle( NS_USER
, $usertext ),
572 $nbytes = wfMsg( 'nbytes', $size );
573 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
575 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
576 . " . . {$userlink} ({$nbytes})";
578 $s .= $this->skin
->commentBlock( $description, $wgTitle );